package com.hongji.lms5xxcommunicator.controller;

import com.hongji.lms5xxcommunicator.entity.RealtimeMetrics;
import com.hongji.lms5xxcommunicator.service.imp.RealtimeMetricsService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/metrics")
public class MetricsController {

    private final RealtimeMetricsService metrics;

    public MetricsController(RealtimeMetricsService metrics) {
        this.metrics = metrics;
    }

    /** 前端每 1s 轮询即可 */
    @GetMapping("/latest")
    public Map<String, Object> latest() {
        RealtimeMetrics m = metrics.latest();
        Map<String, Object> r = new HashMap<>();
        if (m != null) {
            r.put("volume_mm3", m.getVolumeMm3());
            r.put("volume_m3",  m.getVolumeM3());
            r.put("coverage",   m.getCoverage());
            r.put("max_staleness_ms", m.getMaxStalenessMs());
            r.put("ts",         m.getTsMillis());
        }
        return r;
    }
}
